<?php

namespace services\models;


use Yii;

/**
 * This is the model class for table "d_try.order_info".
 *
 * @property integer $order_info_id
 * @property string  $order_sn
 * @property integer $bestry_uid
 * @property integer $type
 * @property string  $create_time
 * @property integer $status
 * @property integer $pay_status
 * @property integer $pay_id
 * @property string  $pay_name
 * @property string  $bank
 * @property string  $goods_amount
 * @property string  $order_amount
 * @property double  $member_money
 * @property double  $reduce_amount
 * @property integer $coupon_id
 * @property string  $coupon_code
 * @property double  $coupon_amount
 * @property double  $ship_amount
 * @property integer $is_invoice
 * @property string  $remark
 * @property string  $update_time
 * @property string  $discount_amount
 * @property integer $try_coin
 * @property integer $source
 * @property integer $ship_amount_try_coin
 */
class OrderInfo extends \common\models\OrderInfo
{

    public static $STATUS_DESC          = [
        0 => '进入订单系统，等待支付',
        1 => '订单已支付，等待确认',
        2 => '订单已确认，配货中',
        3 => '订单已发货',
        4 => '订单已签收',
        5 => '订单完成',
        6 => '订单取消',
    ];
    public static $STATUS_ORDER_CREATED = 0;
    public static $STATUS_PAYED         = 1;
    public static $STATUS_CONFIRM       = 2;
    public static $STATUS_TO_SHIP       = 3;
    public static $STATUS_SIGN_IN       = 4;
    public static $STATUS_ORDER_OK      = 5;
    public static $STATUS_ORDER_CANCEL  = 6;

    public static $PAY_TYPE_DESC = [
        0 => '未支付',
        1 => '支付宝',
        2 => '货到付款',
        3 => '网银支付',
        4 => '余额支付',
        5 => "微信支付",
        6 => '试豆支付',
    ];

    public static $PAY_TYPE_NULL          = 0;
    public static $PAY_TYPE_ALIPAY        = 1;
    public static $PAY_TYPE_PAYAFTER      = 2;
    public static $PAY_TYPE_BANK_INTERNET = 3;
    public static $PAY_TYPE_BALANCE       = 4;
    public static $PAY_TYPE_WEIXIN        = 5;
    public static $PAY_TYPE_COIN          = 6;


    public static $PAY_STATUS         = [0 => '失败', 1 => '成功', 2 => '取消', 3 => '等待'];
    public static $PAY_STATUS_FAIL    = 0;
    public static $PAY_STATUS_SUCCESS = 1;
    public static $PAY_STATUS_CANCEL  = 2;
    public static $PAY_STATUS_WAIT    = 3;

    public static $TYPE_DESC         = ['用户订单', '后台生成订单', '退单'];
    public static $TYPE_MEMBER_ORDER = 0;
    public static $TYPE_ADMIN_ORDER  = 1;
    public static $TYPE_REJECT_ORDER = 2;

    public static $SOURCE_DESC   = ['PC', 'WAP/WEIXIN'];
    public static $SOURCE_PC     = 0;
    public static $SOURCE_MOBILE = 1;

    protected $_productsInCart = [];
    public    $errorMessages   = [];

    protected $_memberInfoBestry = null;
    protected $_score            = null;
    protected $_scoreForProduct  = 0;
    protected $_moneyForProduct  = 0;


    public function beforeSave($insert)
    {
        if ($insert) {
            $this->order_sn      = $this->generateOrderSn();
            $this->pay_id        = static::$PAY_TYPE_NULL;
            $this->create_time   = date('Y-m-d H:i:s');
            $this->pay_name      = '';

        }
        $this->update_time   = date('Y-m-d H:i:s');
        return parent::beforeSave($insert);
    }


    /**
     * @return bool|string
     */
    protected function generateOrderSn()
    {
        $int = $this->redis->incr('serial_order_sn');
        if ($int == 1) {
            $int = static::find()->max('order_info_id');
            $int = $this->redis->incrby('serial_order_sn', $int);
        }
        $order_sn = '1X' . substr(date('000000000') . $int, -9);
        return $order_sn;
    }

    public function getMemberInfoBestry()
    {
        return $this->hasOne(MemberInfoBestry::className(), ['uid' => 'bestry_uid']);
    }

    public function getOrderGoods()
    {
        return $this->hasMany(OrderGoods::className(), ['order_info_id' => 'order_info_id']);
    }

    public function getGoodsTotal()
    {
        return OrderGoods::find()->where(['order_info_id' => $this->order_info_id])->sum('buy_number');
    }

    public function getOrderExpress()
    {
        return $this->hasOne(OrderExpress::className(), ['order_info_id' => 'order_info_id']);
    }


}
